using System;
using System.Windows.Forms;

namespace TangHotKey
{
    /// <summary>
    /// 热键组合类，用于定义热键组合
    /// </summary>
    public class HotKeyCombination
    {
        /// <summary>
        /// 修饰键
        /// </summary>
        public HotKeyManager.ModifierKeys Modifiers { get; set; }

        /// <summary>
        /// 主键
        /// </summary>
        public Keys Key { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="modifiers">修饰键</param>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        public HotKeyCombination(HotKeyManager.ModifierKeys modifiers, Keys key, string description = "")
        {
            Modifiers = modifiers;
            Key = key;
            Description = description;
        }

        /// <summary>
        /// 创建Ctrl+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination Ctrl(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Control, key, description);
        }

        /// <summary>
        /// 创建Alt+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination Alt(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Alt, key, description);
        }

        /// <summary>
        /// 创建Shift+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination Shift(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Shift, key, description);
        }

        /// <summary>
        /// 创建Win+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination Win(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Win, key, description);
        }

        /// <summary>
        /// 创建Ctrl+Alt+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination CtrlAlt(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Control | HotKeyManager.ModifierKeys.Alt, key, description);
        }

        /// <summary>
        /// 创建Ctrl+Shift+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination CtrlShift(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Control | HotKeyManager.ModifierKeys.Shift, key, description);
        }

        /// <summary>
        /// 创建Alt+Shift+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination AltShift(Keys key, string description = "")
        {
            return new HotKeyCombination(HotKeyManager.ModifierKeys.Alt | HotKeyManager.ModifierKeys.Shift, key, description);
        }

        /// <summary>
        /// 创建Ctrl+Alt+Shift+键组合
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="description">描述</param>
        /// <returns>热键组合</returns>
        public static HotKeyCombination CtrlAltShift(Keys key, string description = "")
        {
            return new HotKeyCombination(
                HotKeyManager.ModifierKeys.Control | HotKeyManager.ModifierKeys.Alt | HotKeyManager.ModifierKeys.Shift, 
                key, description);
        }

        /// <summary>
        /// 注册此热键组合
        /// </summary>
        /// <param name="callback">回调函数</param>
        /// <returns>热键ID，失败返回-1</returns>
        public int Register(Action callback)
        {
            return GlobalHotKey.Register(Modifiers, Key, callback, Description);
        }

        /// <summary>
        /// 转换为字符串表示
        /// </summary>
        /// <returns>字符串表示</returns>
        public override string ToString()
        {
            var parts = new System.Collections.Generic.List<string>();
            
            if (Modifiers.HasFlag(HotKeyManager.ModifierKeys.Control))
                parts.Add("Ctrl");
            if (Modifiers.HasFlag(HotKeyManager.ModifierKeys.Alt))
                parts.Add("Alt");
            if (Modifiers.HasFlag(HotKeyManager.ModifierKeys.Shift))
                parts.Add("Shift");
            if (Modifiers.HasFlag(HotKeyManager.ModifierKeys.Win))
                parts.Add("Win");
            
            parts.Add(Key.ToString());
            
            var result = string.Join("+", parts);
            if (!string.IsNullOrEmpty(Description))
                result += $" ({Description})";
            
            return result;
        }
    }
}